package com.itcc.config;

import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

// web容器启动时加载spring容器配置和springMVC容器配置
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

    private static final String ENCODING_UTF_8 = "UTF-8";
    //加载spring容器配置
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{SpringConfig.class};
    }

    //加载springMVC容器配置
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{SpringMvcConfig.class};
    }

    //设置所有请求归属springMVC处理
    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

    //Post请求乱码处理（为Web容器添加过滤器并指定字符集）
    @Override
    protected Filter[] getServletFilters() {
        CharacterEncodingFilter filter = new CharacterEncodingFilter();
        filter.setEncoding(ENCODING_UTF_8);
        filter.setForceEncoding(true); // 强制使用 UTF-8 编码
        return new Filter[]{filter};
    }

}